$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyclassgroup/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true,hidden:true },
			{ label: '班级', name: 'classId', index: 'class_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var classname ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/fyclasstable/info/"+rowdata.classId,
                        success: function(r){
                            classname = r.fyClassTable.classname;

                        }
                    });
                    return classname;
                } },
			{ label: '用户', name: 'userId', index: 'user_id', width: 60,formatter: function(cellvalue, options, rowdata) {
                    var username ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/fyusers/info/"+rowdata.userId,
                        success: function(r){
                            username = r.fyUsers.userName;

                        }
                    });
                    return username;
                } },
			{ label: '发布内容', name: 'content', index: 'content',sortable: true, width: 160 },
			{ label: '发布图片', name: 'imgorvideoulr', index: 'imgorvideoulr', width: 210,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.imgorvideoulr!=null&&rowdata.imgorvideoulr!=''){
                    	var imgarr = rowdata.imgorvideoulr.split(",");
                    	var temp ='';
                    	for(var i=0;i<imgarr.length;i++){
                    		temp +='<img src='+imgarr[i]+' width=\"50\" height=\"50\"/>&nbsp;&nbsp;';
						}
                        return temp;
                    }else{
                        return '';
                    }


                } },
			{ label: '视频封面图片', name: 'coverUrl', index: 'cover_url', width: 70,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.isType == 2&&rowdata.coverUrl!=null&&rowdata.coverUrl!=''){
                        return '<img src='+rowdata.coverUrl+' width="50" height="50"/>';
                    }else{
                        return '';
                    }


                } },
			{ label: '类型', name: 'isType', index: 'is_type', width: 50,formatter: function(item, index) {
                    if (item === 1) {
                        return '<span class="label label-warning">图片</span>';
                    }
                    if (item === 2) {
                        return '<span class="label label-success">视频</span>';
                    }
                } },
			{ label: '点赞数', name: 'greatnum', index: 'greatnum', width: 50 },
			{ label: '评论数', name: 'commentNumble', index: 'comment_numble', width: 50 },
			{ label: '发布时间戳', name: 'creattime', index: 'creattime', width: 80 },
			{ label: '观看数', name: 'watchNumber', index: 'watch_number', width: 50 },
			{ label: '发布时间', name: 'creattimestr', index: 'creattimestr', width: 70 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
        subGrid : true,//开启子表格支持
        //子表格的id；当子表格展开的时候，在主表格中会创建一个div元素用来容纳子表格，subgrid_id就是这个div的id
        subGridRowExpanded : function(subgrid_id, row_id) {//子表格容器的id和需要展开子表格的行id

            vm.bindSubGrid(subgrid_id, row_id);

        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		fyClassGroup: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fyClassGroup = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.fyClassGroup.id == null ? "sys/fyclassgroup/save" : "sys/fyclassgroup/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.fyClassGroup),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyclassgroup/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        deleteItem: function (subgrid_table_id, id) {
            confirm('确定要删除该记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/fyclassgroupcomment/delete/"+id,
                    contentType: "application/json",
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#" + subgrid_table_id.id).trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
		getInfo: function(id){
			$.get(baseURL + "sys/fyclassgroup/info/"+id, function(r){
                vm.fyClassGroup = r.fyClassGroup;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},

		bindSubGrid:function(subgrid_id, collectLineId) {
			var subgrid_table_id;
			subgrid_table_id = subgrid_id + "_t"; // (3)根据subgrid_id定义对应的子表格的table的id

			var subgrid_pager_id;
			subgrid_pager_id = subgrid_id + "_pgr" // (4)根据subgrid_id定义对应的子表格的pager的id

			// (5)动态添加子报表的table和pager
			$("#" + subgrid_id)
				.html(
					"<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+subgrid_pager_id+"' class='scroll'></div>");
			// (6)创建jqGrid对象
			$("#" + subgrid_table_id).jqGrid({
                url : baseURL + 'sys/fyclassgroupcomment/infobygroupid', // (7)子表格数据对应的url，注意传入的contact.id参数
                datatype : "json",
                colModel : [
                    { label: 'id', name: 'id', index: 'id', width: 30, key: true,hidden:true },
                    { label: '用户', name: 'userId', index: 'user_id', width: 60,formatter: function(cellvalue, options, rowdata) {
                            var username ;
                            $.ajax({
                                type: "POST",
                                async:false,
                                url: baseURL + "sys/fyusers/info/"+rowdata.userId,
                                success: function(r){
                                    username = r.fyUsers.userName;

                                }
                            });
                            return username;
                        } },
                    { label: '发布内容', name: 'comment', index: 'comment',sortable: true, width: 160 },
                    { label: '发布图片', name: 'imageUrl', index: 'image_url', width: 210,formatter: function(cellvalue, options, rowdata) {
                            if(rowdata.imageUrl!=null){
                                var imgarr = rowdata.imageUrl.split(",");
                                var temp ='';
                                for(var i=0;i<imgarr.length;i++){
                                    temp +='<img src='+imgarr[i]+' width=\"50\" height=\"50\"/>&nbsp;&nbsp;';
                                }
                                return temp;
                            }else{
                                return '';
                            }


                        } },

                    { label: '发布时间戳', name: 'creatdate', index: 'creatdate', width: 80 },
                    { label: '发布时间', name: 'creatdatestr', index: 'creatdatestr', width: 70 },
                    { label: '操作', name: 'sy', width: 70,formatter: function(cellvalue, options, rowdata) {
                            return "<div class='grid-btn'><a  class='btn btn-primary' onclick='vm.deleteItem("+subgrid_table_id + "," + rowdata.id +")'><i class='fa fa-trash-o'></i>&nbsp;删除</a></div>";
                        } }
                    ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager : subgrid_pager_id,
                mtype : "post",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                postData:{
                    groupid: collectLineId
                },
                gridComplete:function() {
                    //隐藏grid底部滚动条
                    $("#" + subgrid_table_id).closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});

                    var ids = $("#" + subgrid_table_id).jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var id = ids[i];
                        var editBtn = "<div class='grid-btn'> "
                            + "<a  class='btn btn-primary' @onclick='deleteItem("
                            + subgrid_table_id + "," + id
                            + ")'><i class='fa fa-trash-o'></i>&nbsp;删除</a></div>";
                        $("#" + subgrid_table_id).jqGrid(
                            'setRowData', id, {
                                edit: editBtn
                            });
                    }
                }
            });

		}
	}
});